<?php

/**
 * ves short summary.
 *
 * ves description.
 *
 * @version 1.0
 * @author TJF33
 */
namespace common\service\vesrpc;

class ves
{
    /**
     * 获取用户账户
     * @param int $user_id  用户ID
     * @param string $asset 资产类型
	 * @return array    {"error":null,"result":{"USDT":{"available":"0","freeze":"0"},"UPH":{"available":"2000","freeze":"0"}},"id":1603263560}
     */
    public static function balance_query($user_id,...$asset)
	{
        $params = [(int)$user_id];
        foreach ($asset as $v)
		{
            array_push($params,$v);
		}
        return self::do_rpc('balance.query',$params);
	}

	/**
	 * 更新用户账户
	 * @param int $user_id    用户ID
	 * @param string $asset  要更新的资产类型
	 * @param string $business  业务类型 后面可以用来查帐
	 * @param int $business_id  前4项如果相同只会成功一次
	 * @param string $change   数量 正数曾加余额 负数减少余额
	 * @param array $detail 附加信息
	 * @return mixed
	 */
	public static function balance_update($user_id,$asset,$business,$business_id,$change,$detail=[])
	{
		$detail['user_id'] = $user_id;
        $params = [(int)$user_id,$asset,$business,(int)$business_id,''.$change,$detail];
        return self::do_rpc('balance.update',$params);
	}

	/**
	 * 用户账户历史记录
	 * @param int $user_id    用户ID
	 * @param string $asset  资产类型
	 * @param string $business   业务类型
	 * @param int $offset 偏移量
	 * @param int $limit
	 * @param int $start_time
	 * @param int $end_time
	 * @return mixed
	 */
	public static function balance_history($user_id,$asset,$business='',$offset=0,$limit=50,$start_time=0,$end_time=0)
	{
        $params = [(int)$user_id,$asset,$business,$start_time,$end_time,$offset,$limit];
        return self::do_rpc('balance.history',$params);
	}

	/**
	 * 发布限价订单
	 * @param int $user_id    用户ID
	 * @param string $market 市场交易对
	 * @param int $side   1是卖 2是买
	 * @param string $amount 数量
	 * @param string $pride  价格
	 * @param string $taker_fee_rate 买家手续费
	 * @param string $maker_fee_rate 卖家手续费
	 * @param string $source 来源
	 * @return mixed
	 */
	public static function order_put_limit($user_id,$market,$side,$amount,$pride,$taker_fee_rate=0,$maker_fee_rate=0,$source='')
	{
        $params = [(int)$user_id,$market,(int)$side,''.$amount,''.$pride,''.$taker_fee_rate,''.$maker_fee_rate,$source];
        return self::do_rpc('order.put_limit',$params);
	}

	/**
	 * 发布市场价订单
	 * @param int $user_id    用户ID
	 * @param string $market 市场交易对
	 * @param int $side   1是卖 2是买
	 * @param string $amount 数量
	 * @param string $taker_fee_rate 买家手续费
	 * @param string $source 来源
	 * @return mixed
	 */
	public static function order_put_market($user_id,$market,$side,$amount,$taker_fee_rate=0,$source='')
	{
        $params = [(int)$user_id,$market,(int)$side,$amount,''.$taker_fee_rate,$source];
        return self::do_rpc('order.put_market',$params);
	}

	/**
	 * 取消订单
	 * @param int $user_id	用户ID
	 * @param string $market	市场交易对
	 * @param int $order_id	订单ID
	 * @return mixed
	 */
	public static function order_cancel($user_id,$market,$order_id)
	{
        $params = [(int)$user_id,$market,(int)$order_id];
        return self::do_rpc('order.cancel',$params);
	}

	/**
	 * 订单详情
	 * @param mixed $order_id	订单ID
	 * @param mixed $offset
	 * @param mixed $limit
	 * @return mixed
	 */
	public static function order_deals($order_id,$offset,$limit)
	{
        $params = [$order_id,$offset,$limit];
        return self::do_rpc('order.deals',$params);
	}

	/**
	 * 订单列表
	 * @param string $market	市场交易对
	 * @param int $side	1是卖 2是买
	 * @param int $offset
	 * @param int $limit
	 * @return mixed
	 */
	public static function order_book($market,$side,$offset,$limit)
	{
        $params = [$market,(int)$side,(int)$offset,(int)$limit];
        return self::do_rpc('order.book',$params);
	}

	/**
	 * 订单深度
	 * @param string $market	市场交易对
	 * @param int $limit
	 * @param string $interval	间隔
	 * @return mixed
	 */
	public static function order_depth($market,$limit,$interval)
	{
        $params = [$market,(int)$limit,''.$interval];
        return self::do_rpc('order.depth',$params);
	}

	/**
	 * 用户进行中订单列表
	 * @param int $user_id	用户ID
	 * @param string $market	市场交易对
	 * @param mixed $offset
	 * @param mixed $limit
	 * @return mixed
	 */
	public static function order_pending($user_id,$market,$offset,$limit)
	{
        $params = [(int)$user_id,$market,(int)$offset,(int)$limit];
        return self::do_rpc('order.pending',$params);
	}

	/**
	 * 进行中订单详情
	 * @param mixed $market	市场交易对
	 * @param mixed $order_id	订单ID
	 * @return mixed
	 */
	public static function order_pending_detail($market,$order_id)
	{
        $params = [$market,$order_id];
        return self::do_rpc('order.pending_detail',$params);
	}

	/**
	 * 已完成订单列表
	 * @param int $user_id	用户ID
	 * @param string $market	市场交易对
	 * @param int $side		0是全部 1是卖 2是买
	 * @param int $offset
	 * @param int $limit
	 * @param int $start_time
	 * @param int $end_time
	 * @return mixed
	 */
	public static function order_finished($user_id,$market,$side=0,$offset=0,$limit=50,$start_time=0,$end_time=0)
	{
        $params = [(int)$user_id,$market,(int)$start_time,(int)$end_time,(int)$offset,(int)$limit,(int)$side];
        return self::do_rpc('order.finished',$params);
	}

	/**
	 * 已完成订单详情
	 * @param mixed $order_id	订单ID
	 * @return mixed
	 */
	public static function order_finished_detail($order_id)
	{
        $params = [$order_id];
        return self::do_rpc('order.finished_detail',$params);
	}

	/**
	 * 市场价格
	 * @param mixed $market
	 * @return mixed
	 */
	public static function market_last($market)
	{
        $params = [$market];
        return self::do_rpc('market.last',$params);
	}

	/**
	 * 市场价格历史
	 * @param mixed $market	市场交易对
	 * @param mixed $limit
	 * @param mixed $last_id
	 * @return mixed
	 */
	public static function market_deals($market,$limit=50,$last_id=0)
	{
        $params = [$market,$limit,$last_id];
        return self::do_rpc('market.deals',$params);
	}

	/**
	 * 用户价格历史
	 * @param mixed $user_id
	 * @param mixed $market	市场交易对
	 * @param mixed $offset
	 * @param mixed $limit
	 * @return mixed
	 */
	public static function market_user_deals($user_id,$market,$offset=0,$limit=50)
	{
        $params = [(int)$user_id,$market,$offset,$limit];
        return self::do_rpc('market.user_deals',$params);
	}

	/**
	 * 市场K线
	 * @param string $market 市场交易对
	 * @param int $start
	 * @param int $end
	 * @param int $interval
	 * @return mixed	"result": [ [ time,open,close,highest,lowest,volume,amount,market name ] ]
	 */
	public static function market_kline($market,$start,$end,$interval)
	{
        $params = [$market,(int)$start,(int)$end,(int)$interval];
        return self::do_rpc('market.kline',$params);
	}

	/**
	 * 市场状态
	 * @param string $market	市场交易对
	 * @param int $period	秒
	 * @return mixed
	 */
	public static function market_status($market,$period)
	{
        $params = [$market,(int)$period];
        return self::do_rpc('market.status',$params);
	}

	/**
	 * 今日市场状态
	 * @param mixed $market		市场交易对
	 * @return mixed
	 */
	public static function market_status_today($market)
	{
        $params = [$market];
        return self::do_rpc('market.status_today',$params);
	}

	public static function market_list()
	{
        $params = [];
        return self::do_rpc('market.list',$params);
	}

	public static function market_summary()
	{
        $params = [];
        return self::do_rpc('market.summary',$params);
	}

	public static function asset_list()
	{
        $params = [];
        return self::do_rpc('asset.list',$params);
	}

	public static function asset_summary()
	{
        $params = [];
        return self::do_rpc('asset.summary',$params);
	}

	/*请求*/
    public static function do_rpc($method, $params){
        try{
            $NODE_HOST = "http://127.0.0.1:8080";
            //$NODE_HOST = Yii::$app->config->info('VIA_HTTP_API');

            $data = array(
                'jsonrpc' => "2.0",
                'method' => $method,
                'params' => $params,
                'id' => time(),
            );

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $NODE_HOST);
            curl_setopt($ch, CURLOPT_POST, 1);
            $httpHeader[] = 'Content-Type:application/x-www-form-urlencoded';
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data) );
            curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); //处理http证书问题
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_TIMEOUT, 30);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $ret = curl_exec($ch);
            if (!$ret) {
                $rst = [
                    'error'=>[
                        'code'=>1,
                        'message'=>curl_errno($ch)
                    ],
                    'result'=>null
                ];
            }
            else
			{
                $rst = json_decode($ret,true);
            }
            curl_close($ch);
        }
		catch(\Exception $e)
		{
            $rst = [
                'error'=>[
                    'code'=>1,
                    'message'=>$e->getMessage()
                ],
                'result'=>null
            ];
        }
        return $rst;
    }
}